package com.hna.face.bussiness;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.model.FaceVehicle;
import com.hna.face.service.IFaceVehicleService;

@Service
public class WillMessageHandler extends MessageHandler {
	private final static Logger log = LoggerFactory.getLogger(WillMessageHandler.class);
	
	@Autowired
	private IFaceVehicleService service;
	
	/*
	 * 修改掉线信息
	 */
	@Override
	public void run() {
		if(message ==null)
		{
			return;
		}
		if(service ==null) {
			log.error("service is null");
		}
		log.info("service:"+ service);
		byte[] payload = message.getPayload();// 获取二进制文件
		String vehicleNo = new String(payload);
		log.info("payload:"+ vehicleNo);
		vehicleNo = vehicleNo.substring(0,vehicleNo.indexOf(" close"));
		log.info("vehicleNo:"+ vehicleNo);
		Wrapper<FaceVehicle> wraper = new EntityWrapper<>();
		wraper.eq("vehicle_no", vehicleNo);// 按照车辆编号查找
		
		log.info("1:"+ vehicleNo);
		FaceVehicle faceVehicle=null;
		try {
			faceVehicle = service.selectOne(wraper);
		}catch(Exception e)
		{
			log.error("",e);
		}
		if(faceVehicle==null) {
			return;
		}
		log.info("2:"+ vehicleNo);
		log.info("faceVehicle:"+ faceVehicle);
		faceVehicle.setStatus(1);// 修改掉线	
		boolean update = service.update(faceVehicle, wraper);// 按照车辆编号修改
		log.info("willUpdate:"+ update);
	}
}
